from urllib import request
from urllib.parse import unquote,urlencode
import base64
url='http://httpbin.org/post'
headers={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36',
    'Host':'httpbin.org',
    'Chinese1':urlencode({'name':'李宁'}),#设置中文http请求，用url编码格式
    #设置中文http请求，用base64编码格式
    'MyChinese':base64.b64encode(bytes('这是中文HTTP请求',encoding='utf-8')),
    'who':'Python Scrapy'
}
dict={
    'name':'Bill',
    'age':30
}
data=bytes(urlencode(dict),encoding='utf-8')
req=request.Request(url=url,data=data,headers=headers,method="POST")
#通过add_header方法添加中文HTTP请求头，url编码格式
req.add_header('Chinese2',urlencode({'国籍':'中国'}))
response=request.urlopen(req)
#获取服务端的响应信息
value=response.read().decode('utf-8')
print(value)
import json
#将返回值转化为json对象
responseobj=json.loads(value)
#解码url编码格式的http请求
print(unquote(responseobj['headers']['Chinese1']))
#解码url编码格式的http请求
print(unquote(responseobj['headers']['Chinese2']))
#解码base64编码格式的http请求
print(str(base64.b64decode(responseobj['headers']['Mychinese']),'utf-8'))